package com.demo2do.fms.admin.web.controller.report;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.report.report.VehicleAccumulatedMileageReportSearch;
import com.demo2do.fms.admin.entity.search.report.CostRevenueAnalysisSearch;
import com.demo2do.fms.admin.entity.search.report.HeavyNoLoadInstructionSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.authority.OrganizationService;
import com.demo2do.fms.admin.service.report.CostRevenueService;
import com.demo2do.fms.admin.service.report.HeavyNoLoadInstructionService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.report.CostRevenueAnalysis;
import com.demo2do.fms.entity.report.HeavyNoLoadInstruction;

/**
 * 运输收入明细报表预约
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("")
@MenuSetting("menu-report")
public class ReportCostIncomeController {

    private static final Logger logger = LoggerFactory.getLogger(ReportCostIncomeController.class);
    
    @Autowired
    private CostRevenueService costRevenueService;
    @Autowired
    private HeavyNoLoadInstructionService heavyNoLoadInstructionService;
   
    
    /*
     * 
     * 生成结算收入成本分析
     * 
     */
    @MenuSetting("submenu-shipment-cost-seach")
    @RequestMapping(value = "/shipment-cost-seach", method = RequestMethod.GET)
    public ModelAndView costRevenueAnalysisSearch(@Secure Login login, @RequestParam("target") String target) {
    	
		ModelAndView modelAndView = new ModelAndView("report/cost-income/cost-revenue-analysis-seach");
    	
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
    	
    	modelAndView.addObject("target", target);
    	
		return modelAndView;
    }
    
    /*
     * 生成结算收入成本分析 --查询
     * 
     * */
    @MenuSetting("submenu-shipment-cost-seach")
    @RequestMapping(value = "/shipment-cost-seach", method = RequestMethod.GET, params = { "filter" })
    public ModelAndView onCostRevenueAnalysisSearch(CostRevenueAnalysisSearch search,
    												HttpServletRequest request, Page page) {
    	
    	ModelAndView modelAndView=new ModelAndView("report/cost-income/cost-revenue-analysis-list");
    	
        
        int totals = costRevenueService.countCostRevenue(search);
        if (totals > 0) {
            List<CostRevenueAnalysis> costRevenueAnalysis = costRevenueService.listCostRevenueAnalysis(search, page.with(totals));
            modelAndView.addObject("costRevenueAnalysis", costRevenueAnalysis);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
    	
    }
    /**
     * 生成结算收入成本分析导出
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/shipment-cost-export", method = RequestMethod.GET)
    public ModelAndView export(CostRevenueAnalysisSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/shipment-cost-export.xlsx"));

        // 1. list result 
        int totals = costRevenueService.countCostRevenue(search); 
        if (totals > 0) {
            List<CostRevenueAnalysis> costRevenueAnalysis =costRevenueService.listCostRevenueAnalysis(search, page.with(totals));
            modelAndView.addObject("costRevenueAnalysis", costRevenueAnalysis);
        }
               
        // 2. format_util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    /**
     * 生成结算收入成本分析全部导出
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/shipment-cost-on-export", method = RequestMethod.GET)
    public ModelAndView onExport(CostRevenueAnalysisSearch search) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/shipment-cost-on-export.xlsx"));
        List<CostRevenueAnalysis> costRevenueAnalysisAll = costRevenueService.listCostRevenueAnalysisAll(search);
        
            modelAndView.addObject("costRevenueAnalysisAll", costRevenueAnalysisAll);
            FormatUtils fmt = new FormatUtils();
            modelAndView.addObject("fmt", fmt);
        return modelAndView;
        
    }

    
    /*
     * 
     * 生成重空载指令分析
     * */
    @MenuSetting("submenu-shipment-load-instruction-search")
    @RequestMapping(value = "/shipment-load-instruction-search", method = RequestMethod.GET)
    public ModelAndView heavyNoLoadInstruction(@Secure Login login, @RequestParam("target") String target) {
    	
    	ModelAndView modelAndView=new ModelAndView("report/cost-income/heavy-no-load-instruction-search");
    	 modelAndView.addObject("region", login.isRegionUser());
         modelAndView.addObject("orgIds", login.getCurrOrgIds());
     	
     	modelAndView.addObject("target", target);
     	
      
		return modelAndView;
    }
    /*
     * 生成重空载指令分析 --查询
     * 
     * */
    @MenuSetting("submenu-shipment-load-instruction-search")
    @RequestMapping(value = "/shipment-load-instruction-search", method = RequestMethod.GET, params = { "filter" })
    public ModelAndView onHeavyNoLoadInstruction(HeavyNoLoadInstructionSearch search,HttpServletRequest request, Page page) {
    	
    	ModelAndView modelAndView=new ModelAndView("report/cost-income/heavy-no-load-instruction-list");
    	
        
        int totals = heavyNoLoadInstructionService.countHeavy(search);
        
        if (totals > 0) {
            
            List<HeavyNoLoadInstruction> heavyNoLoadInstructions = heavyNoLoadInstructionService.listhHeavyNoLoadInstructions(search, page.with(totals));
            modelAndView.addObject("heavyNoLoadInstructions", heavyNoLoadInstructions);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
    	
    }
    /**
     * 生成重空载指令分析导出
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/shipment-load-instruction-export", method = RequestMethod.GET)
    public ModelAndView exportHeavyNoLoadInstruction(HeavyNoLoadInstructionSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/shipment-load-instruction-export.xlsx"));

        // 1. list result 
        int totals = heavyNoLoadInstructionService.countHeavy(search) ;
        if (totals > 0) {
          List<HeavyNoLoadInstruction> heavyNoLoadInstructions = heavyNoLoadInstructionService.listhHeavyNoLoadInstructions(search, page.with(totals));
            modelAndView.addObject("heavyNoLoadInstructions", heavyNoLoadInstructions);
        }
               
        // 2. format_util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    /**
     * 生成重空载指令分析全部导出
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/shipment-load-instruction-on-export", method = RequestMethod.GET)
    public ModelAndView onExportHeavyNoLoadInstruction(HeavyNoLoadInstructionSearch search) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/shipment-load-instruction-on-export.xlsx"));
         List<HeavyNoLoadInstruction> heavyNoLoadInstructionAll = heavyNoLoadInstructionService.listhHeavyNoLoadInstructionAll(search);
        
            modelAndView.addObject("heavyNoLoadInstructionAll", heavyNoLoadInstructionAll);
            FormatUtils fmt = new FormatUtils();
            modelAndView.addObject("fmt", fmt);
        return modelAndView;
        
    }
}
